package br.ufmg.dees.insane.model.femModel.element;

import java.util.ArrayList;

import br.ufmg.dees.insane.materialMedia.degeneration.Degeneration;
import br.ufmg.dees.insane.materialMedia.degeneration.Representation;
import br.ufmg.dees.insane.shape.Shape;
import br.ufmg.dees.insane.shape.T3;
import br.ufmg.dees.insane.util.IMatrix;
import br.ufmg.dees.insane.util.INaturalCoords;
import br.ufmg.dees.insane.util.IPoint3d;
import br.ufmg.dees.insane.util.IVector;
import br.ufmg.dees.insane.util.extrapolation.TriangularLagrangeanExtrapolation2D;
import br.ufmg.dees.insane.util.numericalIntegration.AreaGaussPoint;
import br.ufmg.dees.insane.util.numericalIntegration.IntegrationPoint;

public class TriangularThinPlate extends ThinPlateElement{
    
	private final static long serialVersionUID = 1L;
	
	/* (non-Javadoc)
     * @see br.ufmg.dees.insane.model.femModel.element.Element#initDegeneration()
     */
	public void initDegenerations()
	{
		int k = 1;
		IntegrationPoint ip = new AreaGaussPoint();
		int cont = this.degenerations.size();
		for(int i = 0; i < cont; i++)
		{
		    Representation representation = new Representation();
			INaturalCoords nc = new INaturalCoords();
			nc.setCsi1(ip.getCoord(cont-1,(3*i)));
			nc.setCsi2(ip.getCoord(cont-1,(3*i+1)));
			representation.setNaturalcoords(nc);
			representation.setWeight(ip.getWeight(cont-1,i));
			representation.setAnalysisModel(this.getAnalysisModel());
			representation.setCm(this.getConstitutiveModel());
			((Degeneration)this.getDegenerations().get(k-1)).setRepresentation(representation);
            ((Degeneration)this.getDegenerations().get(k-1)).setLabel("IP-"+k);
            k++;
		}
	}

	/** Return the IVector containing the cartesian coordinates of a point.
	*@param point The natural coordinates of the point.
	*@return The IVector containing the cartesian coordinates of a point.
	*/
	public IVector getCartesianPointCoords(double[] point) {
		Shape shp = new T3();
	    IMatrix MatrixN = this.analysisModel.getGeometricOperator(shp.getShapeFunction(point,this.getCartesianNodalCoordsMatrix()));
	    IVector pointCoords = new IVector(MatrixN.getNumRow());
		pointCoords.mul(MatrixN, this.getCartesianNodalCoordsVector());
	    return pointCoords;
	}

	
	@Override
	public IVector extrapolatesToNodes(ArrayList natCoords, ArrayList value) {
		TriangularLagrangeanExtrapolation2D extra = new TriangularLagrangeanExtrapolation2D(natCoords,value);
		IMatrix nodeNatCoords = this.Shape.getNaturalNodalCoords();
		IVector values = new IVector(nodeNatCoords.getNumCol());
		for (int i = 0; i < nodeNatCoords.getNumCol(); i++) {
			IPoint3d p = new IPoint3d(nodeNatCoords.getElement(0,i),nodeNatCoords.getElement(1,i));
			values.setElement(i,extra.extrapolateTo(p));
		}
		return values;
	}
	
	
}
